<?php
namespace app\spadmin\controller;
use app\common\controller\Backend;
use Exception;
use Auth\Punycode;
use think\Config;
use think\Db;
use think\helper\Hash;

class Domain extends Backend{
    private $userlist;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Domain');
        $this->userlist = model('user')->where('status',1)->column('username,id,status','id');
        $this->assign('userlist',$this->userlist);
    }

    public function index(){

        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $map['host'] = ['like', '%' . $data['keywords'] . '%'];
            }
            if (isset($data['status']) && $data['status']) {
                $map['status'] = abs($data['status']-2);
            } else {
                $map['status'] = 1;
            }
            $dtype = ['未绑定','转链接','落地页'];
            $list =   $this->model->where($map)->order('create_time desc')->paginate($data['limit'])->toArray();
            foreach ($list['data'] as $key => &$value) {
                if($value['user_id']){
                    $value['user'] = $this->userlist[$value['user_id']]['username'];
                    $value['type'] = $dtype[$value['domain_type']];
                }else{
                    $value['user'] = '未绑定';
                    $value['type'] = '未绑定';
                }
                $today = strtotime(date('Y-m-d'));
                $value['pv'] = Db::name('BrowerLog')->where('create_time','between',[$today,$today+86399])->where('domain_id',$value['id'])->count('id');
                $value['uv'] = Db::name('BrowerLog')->where('create_time','between',[$today,$today+86399])->where('domain_id',$value['id'])->group('ip')->count('id');
            }
            return $this->result($list);
        }
        return $this->view->fetch();
    }
    public function edit($ids=null){
        $row = $this->model->get(['id'=>$ids]);
        if($this->request->isPost()){
            if($row == null){
                $row = $this->model;
            }
            $data = $this->request->post('',[],'strip_tags');
            $data['punycode'] = Punycode::IdnToAscii($data['host']);
            $result = $row->validate()->allowField(true)->save($data,$data['id']?['id'=>$data['id']]:null);
            if($result === false){
                $this->error($row->getError());
            }
            $this->success('操作成功');
        }
        $this->assign('info',$row);
        return $this->view->fetch();
    }
    public function batchadd(){
        if($this->request->isPost()){
            $data = $this->request->post('',[],'strip_tags');
            if(empty($data['domain'])){
                $this->error('请填写域名');
            }
            $host = explode("\n",$data['domain']);
            $domain = [];
            foreach($host as &$h){
                $h = trim($h);
                $domain[] = ['host'=>$h,'punycode'=>Punycode::IdnToAscii($h)]; 
            }
            $model = $this->model->where('host','in',$host)->find();
            if(!empty($model)){
                $this->error('域名有重复:'.$model['host']);
            }
            $this->model->saveAll($domain);
            $this->success('操作成功');
        }
        return $this->view->fetch();
    }
}
